package cn.stylefeng.guns.modular.rec.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.rec.entity.TransportDispatchVolMatch;
import cn.stylefeng.guns.modular.rec.model.params.TransportDispatchVolMatchParam;
import cn.stylefeng.guns.modular.rec.model.result.TransportDispatchVolMatchResult;
import cn.stylefeng.guns.modular.rec.service.TransportDispatchVolMatchService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;


/**
 * 客户订单体积匹配结果表控制器
 *
 * @author zhangzh
 * @Date 2024-10-18 15:55:42
 */
@Controller
@RequestMapping("/transportDispatchVolMatch")
public class TransportDispatchVolMatchController extends BaseController {

    private String PREFIX = "/rec/transportDispatchVolMatch";

    @Autowired
    private TransportDispatchVolMatchService transportDispatchVolMatchService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-10-18
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/transportDispatchVolMatch.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2024-10-18
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/transportDispatchVolMatch_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2024-10-18
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/transportDispatchVolMatch_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-10-18
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TransportDispatchVolMatchParam transportDispatchVolMatchParam) {
        this.transportDispatchVolMatchService.add(transportDispatchVolMatchParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-10-18
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TransportDispatchVolMatchParam transportDispatchVolMatchParam) {
        this.transportDispatchVolMatchService.update(transportDispatchVolMatchParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2024-10-18
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TransportDispatchVolMatchParam transportDispatchVolMatchParam) {
        this.transportDispatchVolMatchService.delete(transportDispatchVolMatchParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2024-10-18
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TransportDispatchVolMatchParam transportDispatchVolMatchParam) {
        TransportDispatchVolMatch detail = this.transportDispatchVolMatchService.getById(transportDispatchVolMatchParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/import.html";
    }

    /**
     * 下载模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcel")
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
        String path = "assets/excel/transportDispatchVolMatch.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("恒安客户订单体积匹配导入模板" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入excel
     */
    @RequestMapping(value = "/importExcel" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importExceNewmethodl(@RequestPart("file") MultipartFile file,Long operateId)  {
        return transportDispatchVolMatchService.getExceldata(file,operateId);
    }


    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-10-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TransportDispatchVolMatchParam transportDispatchVolMatchParam,String condition ) {
        User user = userService.getcurrentUser();
        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(transportDispatchVolMatchParam.getOperateId())){
            transportDispatchVolMatchParam.setOperateId(user.getOperateId());
        }
        return this.transportDispatchVolMatchService.findTranPageBySpec(transportDispatchVolMatchParam,condition);
    }


    @ResponseBody
    @RequestMapping("/listAll")
    public List<TransportDispatchVolMatchResult> listAll(TransportDispatchVolMatchParam transportDispatchVolMatchParam ) {
        User user = userService.getcurrentUser();
        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(transportDispatchVolMatchParam.getOperateId())){
            transportDispatchVolMatchParam.setOperateId(user.getOperateId());
        }
        return this.transportDispatchVolMatchService.findTranListBySpec(transportDispatchVolMatchParam);
    }

    @ResponseBody
    @RequestMapping("/list2")
    public LayuiPageInfo list2(TransportDispatchVolMatchParam transportDispatchVolMatchParam,String condition ) {
        User user = userService.getcurrentUser();
        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(transportDispatchVolMatchParam.getOperateId())){
            transportDispatchVolMatchParam.setOperateId(user.getOperateId());
        }
        return this.transportDispatchVolMatchService.customTran2PageList(transportDispatchVolMatchParam,condition);
    }
    @ResponseBody
    @RequestMapping("/list3")
    public LayuiPageInfo list3(TransportDispatchVolMatchParam transportDispatchVolMatchParam,String condition ) {
        User user = userService.getcurrentUser();
        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(transportDispatchVolMatchParam.getOperateId())){
            transportDispatchVolMatchParam.setOperateId(user.getOperateId());
        }
        return this.transportDispatchVolMatchService.customTran3PageList(transportDispatchVolMatchParam,condition);
    }

    /**
     * 替换体积
     * @param ids
     * @return
     */
    @RequestMapping("/replaceOption")
    @ResponseBody
    public ResponseData replaceOption(@RequestParam("ids") String ids) {
        return this.transportDispatchVolMatchService.replaceOption(ids);
    }

    @RequestMapping("/delAll")
    @ResponseBody
    public ResponseData delAll(@RequestParam("ids") String ids) {
        return this.transportDispatchVolMatchService.delAll(ids);
    }
}


